package com.GlobalPaint.app.ui.Home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.GridViewAdapter;
import com.GlobalPaint.app.Adapter.HomeAdapter;
import com.GlobalPaint.app.Adapter.MyAdapter;
import com.GlobalPaint.app.Adapter.NewHomeAdapter;
import com.GlobalPaint.app.Base.BaseFragment;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.General;
import com.GlobalPaint.app.bean.LoopPicturebean;
import com.GlobalPaint.app.bean.PaletteDetailsBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.ui.App;
import com.GlobalPaint.app.ui.HTML.CommonHTML;
import com.GlobalPaint.app.ui.Home.friend.FriendActivity;
import com.GlobalPaint.app.ui.Home.talent.TalentActivity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.GlobalPaint.app.utils.LoopPicture;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class HomeFragment extends BaseFragment {
    private final String TAG = HomeFragment.class.getSimpleName();
    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    Unbinder unbinder;
    /**
     * Gridview
     */
    private GridView mGridView;
    /**
     * listview
     */
    private XRecyclerView mListView;
    // private List<Schooldtbean> mArrayList;
    //包裹list上边的全部布局的view
    private LinearLayout ll_header;
    private int[] icon = {R.drawable.home_hy, R.drawable.home_sb, R.drawable.home_xy, R.drawable.home_dt,
            R.drawable.home_qy, R.drawable.home_rc, R.drawable.hoem_sq,
            R.drawable.home_cp
    };

    private String[] text = {"朋友圈子", "综合展厅", "水漆学院",
            "行业资讯", "企业展示", "人才宝库", "环保水漆", "环保家具"};
    private List<General> mGeneralList;//要显示的数据集合
    private ListView lvGenerals;//ListView对象
    /**
     * gridview的条目点击事件
     */
    private MyGridViewItemClickListener mItemClickListener;
    private LoopPicture loopPicture;
    private ArrayList<String> mArrayList;
    private NewHomeAdapter newHomeAdapter;
    private LoopPicturebean loopPicturebean;
    private List<LoopPicturebean.DataBean> loopPicturebean_list;
    private String[] img;
    private ImageView iv_saomiao;
    private List<PaletteDetailsBean.DataBean> list;
    private HomeAdapter adapter;
    private String[] title;
    private TextView tv_gd;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, null);
        initView(view);
        initLoad();
        unbinder = ButterKnife.bind(this, view);
        ivGoback.setVisibility(View.GONE);
        tvCommonCenter.setText("全部");
        return view;
    }

    /**
     * 初始化view
     *
     * @param view
     */
    private void initView(View view) {
        mListView = (XRecyclerView) view.findViewById(R.id.listview);
        mListView.setLoadingMoreEnabled(false);
        //mListView.setPullRefreshEnabled(false);
        mListView.setLayoutManager(new LinearLayoutManager(getContext()));
        MyAdapter myAdapter = new MyAdapter();
        mListView.setAdapter(myAdapter);
        //添加头布局
        LayoutInflater lif = (LayoutInflater) App.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View headerView = lif.inflate(R.layout.home_listview_header, null, false);

        //初始化要显示的数据集合---start
        mGeneralList = new ArrayList<General>();
        for (int i = 0; i < icon.length; i++) {
            General general = new General();
            general.setIcon(icon[i]);
            general.setText(text[i]);
            mGeneralList.add(general);
        }

        mGridView = (GridView) headerView.findViewById(R.id.gridview);
        //点击更多
        tv_gd = (TextView)headerView. findViewById(R.id.tv_gd);
        tv_gd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataManager.typeId = 114;
                DataManager.typename = "文章";
               Intent intent = new Intent(getContext(), CommonHTML.class);
                startActivity(intent);
            }
        });
        mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        //设置gridview的适配器,通过构造传过去数据
        mGridView.setAdapter(new GridViewAdapter(mGeneralList));
        loopPicture = (LoopPicture) headerView.findViewById(R.id.loopPicture);
        loopPicture();
      /*  //添加轮播图
        String[] imagesRes = new String[] {
                "drawable://" + R.drawable.ad1,"drawable://" + R.drawable.ad4
        };
        loopPicture.setImageUrl(imagesRes, true);
        if (imagesRes.length == 1) {
            loopPicture.setStopCarousel();
        }
        String[] titles = {"","",
                "","",""};
        loopPicture.setImageTitle(titles);
*/
        //给listview添加header
        mListView.addHeaderView(headerView);
        // 设置listview的适配器
//        mListView.setAdapter(new HomeListViewAdapter(mArrayList));

        mItemClickListener = new MyGridViewItemClickListener();
        //设置gridview的条目点击事件
        mGridView.setOnItemClickListener(mItemClickListener);
        Newest();
    }

    private void loopPicture() {
        String url = Constants.JASON_SERVICE_URL + "info/getInfo?fNodeId=190";
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LoopPicturebean.class,
                new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        loopPicturebean = (LoopPicturebean) o;

                        loopPicturebean_list = loopPicturebean.getData();
                        if(loopPicturebean_list!=null){
                            img = new String[loopPicturebean_list.size()];
                            title = new String[loopPicturebean_list.size()];
                            for (int i = 0; i < loopPicturebean_list.size(); i++) {
                                if (!TextUtils.isEmpty(loopPicturebean_list.get(i).getFSmallImage())) {
                                    img[i] = Constants.JASON_SERVICE_Img + loopPicturebean_list.get(i).getFSmallImage();
                                    title[i] = "";
                                }
                            }

                            loopPicture.setImageUrl(img, true, loopPicturebean_list);
                            if (img.length == 1) {
                                loopPicture.setStopCarousel();
                            }
                            loopPicture.setImageTitle(title);
                        }


                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {

                        String[] imagesRes = new String[]{
                                "drawable://" + R.drawable.ad4
                        };
                        loopPicture.setImageUrl(imagesRes, true, loopPicturebean_list);
                        if (imagesRes.length == 1) {
                            loopPicture.setStopCarousel();

                            loopPicture.setImageTitle(title);
                        }
                    }
                });
    }

    private void Newest() {
        String url = Constants.JASON_SERVICE_URL + "info/queryNewest";
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), PaletteDetailsBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                PaletteDetailsBean paletteBean = (PaletteDetailsBean) o;
                if (paletteBean.getStatus() == 1) {
                    if (paletteBean.getData() != null && paletteBean.getData().size() > 0) {
                        list = paletteBean.getData();
                        adapter = new HomeAdapter(list, getContext());
                        mListView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    } else {
                        Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                }
                mListView.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                if (list != null && list.size() > 0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    mListView.refreshComplete();

                }
            }
        });
    }

    private void initLoad() {
        mListView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url = Constants.JASON_SERVICE_URL + "info/queryNewest";
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), PaletteDetailsBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        PaletteDetailsBean paletteBean = (PaletteDetailsBean) o;
                        if (paletteBean.getStatus() == 1) {
                            if (paletteBean.getData() != null && paletteBean.getData().size() > 0) {
                                list = paletteBean.getData();
                                adapter = new HomeAdapter(list, getContext());
                                mListView.setAdapter(adapter);
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                        }
                        mListView.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        if (list != null && list.size() > 0) {
                            list.clear();
                            Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                            adapter.notifyDataSetChanged();
                            mListView.refreshComplete();

                        }
                    }
                });
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    class MyGridViewItemClickListener implements AdapterView.OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = null;
            switch (position) {
                //圈子
                case 0:
                  /*  String ChatToUserName="wangp";
                    intent = new Intent(getContext(), ChatActivity.class);
                    intent.putExtra("from",ChatToUserName);
                    startActivity(intent);*/
                    intent = new Intent(getContext(), FriendActivity.class);
                    startActivity(intent);
                    break;
                //色板样品
                case 1:
                    DataManager.typeId = 121;
                    DataManager.typename = "图集";//图集
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;
                //技术学院
                case 2:
                    DataManager.typeId = 180;
                    DataManager.typename = "图集";//图集
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;
                //动态资讯
                case 3:
                    DataManager.typeId = 114;
                    DataManager.typename = "文章";
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;
                //企业展示
                case 4:
                    DataManager.typeId = 124;
                    DataManager.typename = "文章";
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;
                //人才库
                case 5:
                    intent = new Intent(getContext(), TalentActivity.class);
                    startActivity(intent);
                    break;
                //水性木器漆专区
                case 6:
                    DataManager.typeId = 127;
                    DataManager.typename = "文章";
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;
                //涂料产品推广专区
                case 7:
                    DataManager.typeId = 150;
                    DataManager.typename = "文章";
                    intent = new Intent(getContext(), CommonHTML.class);
                    startActivity(intent);
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //页面不可见的时候移除
      //  loopPicture.setStopCarousel();
    }

}
